<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/11/0011
 * Time: 9:09
 */

namespace app\sports_admin\model;


use think\Model;
use traits\model\SoftDelete;

class Equipment extends Model
{
    use SoftDelete;
    protected $table = 'device';

    protected $pk = 'id';

    protected $autoWriteTimestamp = true;

    //查看设备绑定的学校   1对1
    public function school()
    {
        return $this->belongsTo('school','school_id','school_id')->field('school_id,school_name');
    }
    //查看设备绑定的家长  1对1
    public function parents()
    {
        return $this->belongsTo('parents','parent_id','id')->field('id,name,phone');
    }

    /**
     * 获取设备列表
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
     * @param $schoolId 学校id
     */
    public static function getEquipmentList($page,$rows,$sort,$sortOrder,$searchName,$schoolId)
    {
        $result =  self::with('school,parents')->where(function($query)use($searchName,$schoolId){
            if($searchName){
                $query->where('number','like',"%{$searchName}%");
            }
            if($schoolId){
                $query->where('school_id',"{$schoolId}");
            }
        })->order("{$sort}","{$sortOrder}")->limit($rows)->page($page)->select();
        $total =  self::with('school,parents')->where(function($query)use($searchName,$schoolId){
            if($searchName){
                $query->where('number','like',"%{$searchName}%");
            }
            if($schoolId){
                $query->where('school_id',"{$schoolId}");
            }
        })->count();
        return json(['rows'=>$result,'total'=>$total]);
    }

}